package me.alide.modules.wechart.cp.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import lombok.RequiredArgsConstructor;

import me.alide.modules.wechart.cp.service.GovPeopleService;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.json.GsonParser;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;

import java.util.List;


@RequiredArgsConstructor
public class GovPeopleServiceImpl implements GovPeopleService {

    private final WxCpService mainService;

    private final String PATROL_GRID_GET = "/cgi-bin/report/patrol/get_grid_info";

    private final String RESIDENT_GRID_GET = "/cgi-bin/report/resident/get_grid_info";

    private final String PATROL_REPORT_LIST = "/cgi-bin/report/patrol/get_order_list";

    private final String RESIDENT_REPORT_LIST = "/cgi-bin/report/resident/get_order_list";


    private final String PATROL_CORP_STATUS = "/cgi-bin/report/patrol/get_corp_status";

    private final String PATROL_CATEGORY_STATISTIC =  "/cgi-bin/report/patrol/category_statistic";

    private final String RESIDENT_CORP_STATUS = "/cgi-bin/report/resident/get_corp_status";


    private final String RESIDENT_CATEGORY_STATISTIC = "/cgi-bin/report/resident/category_statistic";



    /*@Override
    public List<WxGrid> patrolListGrid() throws WxErrorException {
        String url = this.mainService.getWxCpConfigStorage().getApiUrl(PATROL_GRID_GET);
        String responseContent = this.mainService.get(url, null);
        JsonObject tmpJson = GsonParser.parse(responseContent);
        return WxCpGsonBuilder.create()
                .fromJson(
                        tmpJson.get("grid_list"),
                        new TypeToken<List<WxGrid>>() {
                        }.getType()
                );
    }

    @Override
    public List<WxGrid> residentListGrid() throws WxErrorException {
        String url = this.mainService.getWxCpConfigStorage().getApiUrl(RESIDENT_GRID_GET);
        String responseContent = this.mainService.get(url, null);
        JsonObject tmpJson = GsonParser.parse(responseContent);
        return WxCpGsonBuilder.create()
                .fromJson(
                        tmpJson.get("grid_list"),
                        new TypeToken<List<WxGrid>>() {
                        }.getType()
                );
    }


    @Override
    public JSONArray listPatrolReportList() throws WxErrorException {
        String url = this.mainService.getWxCpConfigStorage().getApiUrl(PATROL_REPORT_LIST);
        String responseContent = this.mainService.get(url, null);
        JSONObject jb = JSON.parseObject(responseContent);
        return jb.getJSONArray("order_list");
    }

    @Override
    public JSONArray listResidentReportList() throws WxErrorException {
        String url = this.mainService.getWxCpConfigStorage().getApiUrl(RESIDENT_REPORT_LIST);
        String responseContent = this.mainService.get(url, null);
        JSONObject jb = JSON.parseObject(responseContent);
        return jb.getJSONArray("order_list");
    }


    @Override
    public JSONObject patrolCorpStatus() throws WxErrorException {
        String url = this.mainService.getWxCpConfigStorage().getApiUrl(PATROL_CORP_STATUS);
        String responseContent = this.mainService.get(url, null);
        return JSON.parseObject(responseContent);
    }



    @Override
    public JSONArray patrolCategoryStatistic() throws WxErrorException {
        String url = this.mainService.getWxCpConfigStorage().getApiUrl(PATROL_CATEGORY_STATISTIC);
        String responseContent = this.mainService.get(url, null);
        JSONObject jb = JSON.parseObject(responseContent);
        return jb.getJSONArray("dashboard_list");
    }

    @Override
    public JSONObject residentCorpStatus() throws WxErrorException {
        String url = this.mainService.getWxCpConfigStorage().getApiUrl(RESIDENT_CORP_STATUS);
        String responseContent = this.mainService.get(url, null);
        return JSON.parseObject(responseContent);
    }


    @Override
    public JSONArray residentCategoryStatistic() throws WxErrorException {
        String url = this.mainService.getWxCpConfigStorage().getApiUrl(RESIDENT_CATEGORY_STATISTIC);
        String responseContent = this.mainService.get(url, null);
        JSONObject jb = JSON.parseObject(responseContent);
        return jb.getJSONArray("dashboard_list");
    }*/
}
